# 自定义栈异常
class StackError(Exception):
    pass


# 基于列表实现顺序栈
class SStack:
    def __init__(self):
        # 约定列表的最后一个元素为栈顶
        self._elems = []

    def top(self):
        """
        获取栈顶元素
        :return:
        """
        if not self._elems:
            raise StackError("stack is empty")
        return self._elems[-1]

    def is_empty(self):
        """
        判断栈是否为空
        :return:
        """
        return st._elems == []

    def push(self, elem):
        """
        入栈
        :param elem:
        :return:
        """
        self._elems.append(elem)

    def pop(self):
        """
        出栈
        :return:
        """
        if not self._elems:
            raise StackError("stack is empty")
        return self._elems.pop()

    def is_double(self, value):
        """
        判断一串字符串是否有配对的括号
        :param value: str类型，待输入的字符串
        :return:
        """
        left = ("(", "[", "{")
        right = (")", "]", "}")
        double = ("()", "[]", "{}")
        for i in value:
            if i in left:
                self.push(i)
            if i in right:
                if self.is_empty():
                    print("不配对")
                    return False
                a = self.pop() + i
                if a in double:
                    continue
                else:
                    print("不配对")
                    return False
        return True



